-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MVJ-112: area search attachments: hide user and attachment path from API response in public #819
MVJ-112: area search attachments: hide user and attachment path from API response in public #819
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about forms attachment?
7cde77f
to
9863eb9
Compare
Now added. |
Looks good! It could be useful to add a comment, or add the list of fields in a named variable to indicate what is being done and why |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the approve to request changes, realized that this should also include tests.
Created variables for the excluded fields and commented. They turned out to be useful in the unit tests as well. |
forms/tests/test_api.py
Outdated
|
||
|
||
@pytest.mark.django_db | ||
def test_area_search_attachment_post_public( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this test exist in the areasearch app?
@@ -430,6 +432,97 @@ def test_attachment_delete( | |||
assert os.path.isfile(file_path) is False | |||
|
|||
|
|||
@pytest.mark.django_db | |||
def test_attachment_post_public( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name could clarify what is it testing for, and enhanced with comments. It is not very clear what is being tested, the name indicates it is just to test that the endpoint works
) | ||
== 0 | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about testing the areasearch endpoint, that it doesnt return the undesired fields?
forms/tests/test_api.py
Outdated
response = admin_client.post(url, data=payload, content_type="application/json") | ||
|
||
# When an answer is created, the HTTP response should not contain attachments | ||
assert response.json().get("attachments") is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could also be checked that the key does not exist, this makes it seem a little bit that test expects the key to be there and value to be None
utils/viewsets/mixins.py
Outdated
@@ -165,3 +165,9 @@ def update(self, request, *args, **kwargs): | |||
@action(methods=["get"], detail=True) | |||
def download(self, request, pk=None, file_field: str | None = None): | |||
return FileDownloadMixin.download(self, request, pk, file_field=file_field) | |||
|
|||
|
|||
class DisablePublicDownloadMixin: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a good addition! What do you think about the following approach: Implement two versions of the FileMixin, one that allows only upload, and one that allows upload and download? There is potential that having this disable makes it a bit messy regarding the mixins. If there were two options to choose from, it might be easier to realize that there is this difference
Would it make sense to also test the serializers? Hopefully that would be rather simple, but would confirm they work as intended down the line. |
assert os.path.isfile(file_path) is True | ||
response = admin_client.delete(url) | ||
|
||
assert response.status_code == 405 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 405 could come from from rest_framework import status
, status.HTTP_405_METHOD_NOT_ALLOWED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@henrinie-nc That is a good and refined approach because it makes sure that the status code is exactly the same what Django Rest Framework uses, but I'm not sure if it's an improvement here over a plain understandable status code number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it doesnt add anything to the comparison, but I think it helps humans reading it to realize what is expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My change requests are about test understandability. I didn't find holes in the implementation when I tested it locally. Good work! Some of Henri's comments should be addressed or replied to.
assert os.path.isfile(file_path) is True | ||
response = admin_client.delete(url) | ||
|
||
assert response.status_code == 405 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@henrinie-nc That is a good and refined approach because it makes sure that the status code is exactly the same what Django Rest Framework uses, but I'm not sure if it's an improvement here over a plain understandable status code number.
4552312
to
a2643e3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My grievances have been addressed. We will fix merge conflicts later today, because my yesterday's merge touched the same files.
test_attachment_upload: remove docstring test forms attachment upload and download: rename functions and improve comments excluded attachment fields: improve commenting use pytest raises improve assert test serializers
1d75dba
to
6004e28
Compare
6004e28
to
4b646a7
Compare
With these changes, the API response to the POST requests will hide sensitive information about the attachments.